package xyz.xtt.pay.mq;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.pay.dto.OrderRefundMessage;
import xyz.xtt.pay.service.INftOrderService;

/**
 * @author dale
 * @date 2025/1/10
 **/
@RefreshScope
@Slf4j
@Component
@RocketMQMessageListener(topic = "${rocketmq.order.refund.topic}", consumerGroup = "${rocketmq.consumer.group.payrefund}")
public class OrderRefundConsumer implements RocketMQListener<String> {
    @Autowired
    private INftOrderService nftOrderService;

    @Override
    public void onMessage(String message) {
        if (StringUtils.isBlank(message) || !JSONUtil.isTypeJSON(message)){
            log.warn("OrderRefundConsumer received invalid message:{}",message);
            return;
        }
        log.info("OrderRefundConsumer receive message:{}",message);
        OrderRefundMessage orderRefundMessage = JSONUtil.toBean(message,OrderRefundMessage.class);
        nftOrderService.refundOrder(orderRefundMessage);
    }
}
